import { ref, onMounted } from 'vue'
import { postApi } from '@/api/public/post'
import type { Post } from '@/types/post'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export const useGuide = () => {
  const posts = ref<Post[]>([])
  const router = useRouter()

  const fetchPosts = async () => {
    try {
      const response = await postApi.getList()
      console.log('文章列表响应:', response)
      if (response.data.code === 200) {
        posts.value = response.data.data.content
        console.log('处理后的文章列表:', posts.value)
      }
    } catch (error) {
      console.error('获取文章列表失败:', error)
      ElMessage.error('获取文章列表失败')
    }
  }

  const goToDetail = (id: number) => {
    router.push(`/guide/${id}`)
  }

  onMounted(() => {
    fetchPosts()
  })

  return {
    posts,
    goToDetail
  }
} 